package pl.polsl.mmm.projectcomm;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import pl.polsl.mmm.projectcomm.util.bluetooth.AcceptThread;
import pl.polsl.mmm.projectcomm.util.bluetooth.ConnectedThread;


public class ServerActivity extends Activity {

    BluetoothAdapter mBluetoothAdapter;
    final int REQUEST_ENABLE_BT = 1010;
    ServerActivity me;
    AcceptThread acceptThread;
    TextView infoText;

    private final Handler handler = new Handler() {

        public void handleMessage(Message msg) {

            String response = msg.getData().getString("message");

            if ((null != response)) {

                infoText.setText(response);
//                Toast.makeText(getBaseContext(), response, Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server);
        me = this;
        infoText = (TextView)findViewById(R.id.infoText);

        try {

            // Pobranie obiektu adaptera bluetooth
            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (mBluetoothAdapter == null) {
                throw new RuntimeException("No bluetooth adapter");
            }

            // Sprawdzenie, czy bluetooth jest włączone
            if (!mBluetoothAdapter.isEnabled()) {
                // Pytanie o pozwolenie na włączenie bluetooth
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                this.finish();
            }

            if(!mBluetoothAdapter.isEnabled())
                this.finish();

            // Utworzenie obiektu AcceptThread oczekującego na połączenie w nowym wątku
            acceptThread = new AcceptThread(me, mBluetoothAdapter, handler);
            acceptThread.start();

        } catch (Exception e) {
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.server, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

